﻿using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.Html;
using System;
using System.Collections.Generic;

namespace SpiderStudy.WebSite.Baidu
{
    internal class BaiJiaSearch
    {
        private string searchApi = "https://www.baidu.com/s?wd=site%3Abaijiahao.baidu.com%20{0}&pn={1}";

        /// <summary>
        /// 搜索API wd是关键词，需要encode pn是页面，从0开始 每次加10
        /// </summary>
        public string SearchApi { get => searchApi; set => searchApi = value; }

        /// <summary>
        /// 指定关键词，获取保存相关数据
        /// </summary>
        /// <param name="StartPage"></param>
        /// <param name="EndPage"></param>
        /// <param name="TAG"></param>
        public void SpiderGetContentData(int StartPage, int EndPage, string TAG)
        {
            List<string> ArticleList = SearchList(StartPage, EndPage, TAG);
            List<string> TitleList = new List<string>();
            List<string> ContentList = new List<string>();

            int DowndloadCompleted = 0;
            foreach (string SingleArticleLink in ArticleList)
            {
                Console.WriteLine("扫描{0} 总数{1}", ++DowndloadCompleted, ArticleList.Count);
                WebSiteHelper.ArticleInfo TempArticleInfo = AnalysisSingleContent(SingleArticleLink, TAG);
                if (TempArticleInfo.Title1 != "")
                    new WebSiteHelper().SaveSpiderData(string.Format("{0}.txt",
                        TextHelper.ReplaceBadChar(TAG)),
                        "Baidu\\Title", new List<string>() { TempArticleInfo.Title1 });
                if (TempArticleInfo.Content1 != "" && TempArticleInfo.Content1.Length < 300)
                    new WebSiteHelper().SaveSpiderData(string.Format("{0}.txt",
                        TextHelper.ReplaceBadChar(TAG)),
                        "Baidu\\Content", new List<string>() { TempArticleInfo.Content1 });
            }
            new WebSiteHelper().DataToRepeat(string.Format("{0}.txt",
                        TextHelper.ReplaceBadChar(TAG)),
                        "Baidu\\Title");
            new WebSiteHelper().DataToRepeat(string.Format("{0}.txt",
                    TextHelper.ReplaceBadChar(TAG)),
                    "Baidu\\Content");
        }

        /// <summary>
        ///  分析单个文章文本
        /// </summary>
        /// <param name="line"></param>
        /// <param name="tAG"></param>
        /// <returns></returns>
        private WebSiteHelper.ArticleInfo AnalysisSingleContent(string SingleArticleLink, string TAG)
        {
            String Shtml = WebHelper.HttpGet(SingleArticleLink);
            String Title = HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(Shtml, "//title"));
            if (Title.IndexOf(TAG) < 0)//如果标题中不包含关键词，则不做记录
                Title = "";
            string Content = "";
            foreach (string Line in HtmlHelper.GetTextValueList(Shtml, "//div[@class='article-content']/p"))
            {
                String RegexFilter = TAG.Replace(" ", ".*?");

                if (TextHelper.RegexText(Line, RegexFilter).Length > 0)
                    Content += "\r\n" + Line;
            }

            return new WebSiteHelper.ArticleInfo { Title1 = Title, Content1 = Content, Shtml1 = "" };
        }

        /// <summary>
        /// 搜索list返回结果
        /// </summary>
        /// <param name="StartPage"></param>
        /// <param name="EndPage"></param>
        /// <param name="TAG"></param>
        /// <returns></returns>
        public List<string> SearchList(int StartPage, int EndPage, string TAG)
        {
            List<string> TempList = new List<string>();
            for (; StartPage < EndPage; StartPage++)
            {
                string TempApi = string.Format(SearchApi, new TextHelper().UrlEncodeToUtf8(TAG), StartPage * 10);
                foreach (string Line in HtmlHelper.GetLinkVlaueList(WebHelper.HttpGet(TempApi), "//div[@class='result c-container ']/h3/a"))
                {
                    TempList.Add(Line);
                }

                Console.WriteLine(TempApi);
            }

            return TempList;
        }
    }
}